package edu.whcp.comp2301.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import edu.whcp.comp2301.dao.NewsDao;
import edu.whcp.comp2301.pojo.Category;
import edu.whcp.comp2301.pojo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/list")
public class NewsController {
    @Autowired
    private NewsDao newsDao;

    @RequestMapping("/news")
    public ModelAndView list() {
        List<News> newsList = newsDao.getNewsList(null, null);
        return new ModelAndView("backend/news-management", "newsList", newsList);
    }

    @RequestMapping(value = "/search/news",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<News> search(@RequestParam String category,@RequestParam String title) {
        return newsDao.getNewsList(category,title);
    }

    @RequestMapping(value = "/add/news", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer addNews(@RequestBody News news) {
        newsDao.addNews(news);
        return 1;
    }

    @RequestMapping(value = "/del/news/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer delete(@PathVariable String id) {
        return newsDao.delNews(id);
    }

    @RequestMapping(value = "/edit/news", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Integer edit(@RequestBody News news) {
        newsDao.editNews(news);
        return 1;
    }

    @RequestMapping(value = "/get/news", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<News> listById(@RequestParam String id) {
        return newsDao.getNews(id);
    }
}
